package com.zhenyi.sms;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.cenjiawen.common.IndexController;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.utils.MobileUtils;
import com.utils.PathUtils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

public class SMSController extends IndexController {

	private final String key = "SMS_Key";
	private final String KEY_FREQUENCE = "KEY_FREQUENCE";

	public void test() {
		String to = getPara("to");
		if (StrKit.isBlank(to)) {
			renderFailure("no number");
			return;
		}
		// if (!to.contains("+86")) {
		// renderFailure();
		// return;
		// }
		if (!MobileUtils.isChinaPhoneLegal(to)) {
			renderFailure("nunber illegal");
			return;
		}
		Cache cache = Redis.use();
		String val = cache.get(key);
		if (val != null && !to.equals(val)) {
			renderFailure("send too fast");
			return;
		}
		if (cache.get(KEY_FREQUENCE) != null) {
			renderFailure("30s for one message");
			return;
		}
		cache.setex(key, 180, to);
		cache.setex(KEY_FREQUENCE, 30, to);
		new Thread(new Runnable() {
			public void run() {
				send("+86" + to, "Hello 你好 ", true, "http://zhenyi.asuscomm.com:333/sendMessage.srv");
			}
		}).start();
		renderSuccess();
	}

	public void send() {
		String content = getPara("content");
		String to = getPara("to");
		String ip = getPara("ip");
		int check = getParaToInt("check", 0);
		if (StrKit.isBlank(content)) {
			renderFailure();
			return;
		}
		if (StrKit.isBlank(to)) {
			renderFailure();
			return;
		}
		send(to, content, check == 0, ip);
		renderSuccess();
	}

	private void send(String mobile, String content, boolean isSend, String ip) {
		String number = mobile;
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("hashid", number);
		paramMap.put("reqUID", "z1al");
		paramMap.put("recipients", number);
		paramMap.put("file-name", "");
		paramMap.put("text", "abc");
		String url = "http://" + ip + ":333/sendMessage.srv";
		if (ip.contains("http://")) {
			url = ip;
		}
		System.out.println(url);

		// url = "http://192.168.50.158:333/sendMessage.srv";
		// System.out.println(url);
		// HttpRequest req = HttpUtil.createPost(url);
		// req = req.form(paramMap);
		// System.out.println(req);
		// System.out.println(req.execute().body());
		// String result2 = HttpRequest.post(url)// 头信息，多个头信息多次调用此方法即可
		// .form(paramMap)// 表单内容
		// .timeout(20000)// 超时，毫秒
		// .execute().body();

		HttpRequest request = HttpUtil.createPost(url);
		// request.contentType(HttpRequest.TIMEOUT_DEFAULT)
		request.form("hashid", number);
		request.form("reqUID", "z1al");
		request.form("recipients", number);
		request.form("file-name", "");
		request.form("text", content);
		String path = PathUtils.getFile("logs", "abc");
		// FileWriter writer = new FileWriter(path);
		// writer.append("sbc");
		File ff = FileUtil.file(path);
		if (isSend) {
			request.form("file", new File(""));
		} else {
			request.form("file", new File(path));
		}
		// request.form("file", "");
		boolean isSuc = false;
		request.execute().body();
		isSuc = true;
		// if (!isSuc) {
		// ThreadUtil.safeSleep(500);
		// try {
		// request.execute().body();
		// isSend = true;
		// } catch (Exception e) {
		// }
		// }
		// if (!isSuc) {
		// ThreadUtil.safeSleep(500);
		// try {
		// request.execute().body();
		// isSend = true;
		// } catch (Exception e) {
		// }
		// }
		String msg = mobile + (isSuc ? "成功" : "失败");
		// fileWriter.append(msg + "\n");
		// System.out.println(msg);
		if (isSuc) {

			// check(mobile);
		}
	}

}
